<!DOCTYPE HTML>
<html>

<head>
    <title>behaviour-Force</title>
    <meta name="viewport" id="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <meta charset="utf-8">
    <style type="text/css">
        body {
            font-family: Monospace;
            background-color: #f0f0f0;
            margin: 0px;
            overflow: hidden;
        }
        
        #container {
            width: 100%;
            height: 100%;
        }
        
        #testCanvas {
            background: #000;
        }
    </style>
</head>

<body>
    <div id="container">
        <canvas id="testCanvas"></canvas>
    </div>
    <script src="../../lib/requestAnimationFrame.min.js"></script>
    <script src="../../lib/stats.min.js"></script>
    <script src="//localhost:3001/build/proton.js"></script>
    <script>
        var canvas;
        var context;
        var proton;
        var renderer;
        var emitter;
        var stats;
        var forceBehaviour;

        main();

        function main() {
            canvas = document.getElementById("testCanvas");
            canvas.width = window.innerWidth;
            canvas.height = window.innerHeight;
            context = canvas.getContext('2d');
            addStats();

            createProton();
            tick();
            canvas.addEventListener('mousemove', mousemoveHandler, false);
        }

        function addStats() {
            stats = new Stats();
            stats.setMode(2);
            stats.domElement.style.position = 'absolute';
            stats.domElement.style.left = '0px';
            stats.domElement.style.top = '0px';
            document.getElementById('container').appendChild(stats.domElement);
        }

        function createProton() {
            proton = new Proton;
            emitter = new Proton.Emitter();
            emitter.rate = new Proton.Rate(new Proton.Span(5, 10), new Proton.Span(.2, .5));

            emitter.addInitialize(new Proton.Mass(1));
            emitter.addInitialize(new Proton.Body(['image/star.png', 'image/star2.png', 'image/star3.png']));
            emitter.addInitialize(new Proton.Life(10, 15));
            emitter.addInitialize(new Proton.Velocity(new Proton.Span(1, 2), new Proton.Span(0, 360), 'polar'));

            forceBehaviour = new Proton.Force(0, 0);
            emitter.addBehaviour(new Proton.Rotate(Proton.getSpan(0, 180), 2.5, 'add'));
            emitter.addBehaviour(forceBehaviour, new Proton.Gravity(.9));
            emitter.addBehaviour(new Proton.RandomDrift(10, 10, .05));
            emitter.addBehaviour(new Proton.CrossZone(new Proton.RectZone(-50, -50, canvas.width + 100, canvas.height + 100), 'dead'));
            emitter.addBehaviour(new Proton.Scale(Proton.getSpan(.3, .6), Proton.getSpan(1.5, 2)));

            emitter.p.x = canvas.width / 2;
            emitter.p.y = canvas.height / 2;
            emitter.emit();
            proton.addEmitter(emitter);

            renderer = new Proton.CanvasRenderer(canvas);
            renderer.onProtonUpdate = function() {
                context.fillStyle = "rgba(0, 0, 0, 0.05)";
                context.fillRect(0, 0, canvas.width, canvas.height);
            };
            proton.addRenderer(renderer);

            window.onresize = function(e) {
                canvas.width = window.innerWidth;
                canvas.height = window.innerHeight;
                emitter.p.x = canvas.width / 2;
                emitter.p.y = canvas.height / 2;
            }
        }

        function mousemoveHandler(e) {
            if (e.layerX || e.layerX == 0) {
                _x = e.layerX - canvas.width / 2;
                _y = e.layerY - canvas.height / 2;
            } else if (e.offsetX || e.offsetX == 0) {
                _x = e.offsetX - canvas.width / 2;
                _y = e.offsetY - canvas.height / 2;
            }

            var R = 3.9;
            var tha = Math.atan2(_y, _x);
            var x = Math.cos(tha) * R;
            var y = Math.sin(tha) * R;
            forceBehaviour.reset(x, y);
        }

        function tick() {
            requestAnimationFrame(tick);
            stats.begin();

            proton.update();
            proton.stats.update(2, document.body);
            stats.end();
        }
    </script>
</body>

</html>